<?php
/**
 * empleados
 *
 * empleados.php
 * @author Hector Ruiz
 * @version 1.0
 * @copyright Hector Ruiz, 13 October, 2011
 * @package default
 **/

class Empleados extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model("menu_model");
        $this->load->model("empleados_model");

        //Only logged in users are alowed to access this controller
        if($this->session->userdata('logged_in') == FALSE) redirect('login');
    }

    function index() {
        $data = $this->menu_model->general("Empleados");
        $data['app_menu'] = $this->menu_model->return_link_menu('cmdb');
        $data['site_name'] = "CMDB";
        $data['content_html'] = $this->dashboard();
        $js = '
        
                $(document).ready( function () {
                $("#example").dataTable( {
                    "aaSorting":[[1, "asc"]],
                    "bJQueryUI": true,
                    "sPaginationType": "full_numbers",
                    "sDom": \'<"H"Tfr>t<"F"ip>\',
                    "oTableTools": {
                    "sSwfPath": "'.$this->config->item('swf_path').'copy_cvs_xls_pdf.swf",
                    "aButtons": [
                        {
                            "sExtends": "copy",
                            "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 8]
                        },
                        {
                            "sExtends": "csv",
                            "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 8]
                        },
                        {
                            "sExtends": "xls",
                            "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 8]
                        },
                        {
                            "sExtends": "pdf",
                            "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 8]
                        }
                    ]
                    }
                } );
            } );
            
            ';
        $data['js'] = $js;
        $this->load->view('template', $data);
    }

    function dashboard() {
        $images_path = $this->config->item('images_path');
        $order = array('apellido_paterno', 'nombre_1');
        $html = "";
        $html.= "<table id='example' width='100%' class='ui-widget-header'>";
        $html.= "<thead>";
        $html.= "<tr>";
        $html.= "<th>REGION</th>";
        $html.= "<th>STVC</th>";
        $html.= "<th>PRIMER NOMBRE</th>";
        $html.= "<th>SEGUNDO NOMBRE</th>";
        $html.= "<th>APELLIDO PATERNO</th>";
        $html.= "<th>APELLIDO MATERNO</th>";
        $html.= "<th>USUARIO DE RED</th>";
        $html.= "<th>DEPARTAMENTO</th>";
        $html.= "<th>PUESTO</th>";
        $html.= "<th>EXTENSION</th>";
        $html.= "<tH colspan=2 size='6%'>".anchor('empleados/add','<img src="'.$images_path.'New.png" border="0" width="22" height="22" />')."</th>";
        $html.= "</tr>";
        $html.= "</thead>";
        $html.= "<tbody class='ui-widget-container'>";
        $values = $this->empleados_model->get_list('empleados','',$order);
        if(!empty($values)) {
            foreach($values as $k=>$v) {
                $id = $values[$k]['usuario_red'];
                $html.= "<tr>";
                $evals = $this->empleados_model->read('catalogo_regiones', 'id', $values[$k]['region']);
                $html.= "<td align='center'>".$evals[0]['region']."</td>";
                $evals = $this->empleados_model->read('catalogo_ciudades', 'id', $values[$k]['stvc']);
                $html.= "<td align='center'>".$evals[0]['ciudad']."</td>";
                $html.= "<td align='center'>".$values[$k]['nombre_1']."</td>";
                $html.= "<td align='center'>".$values[$k]['nombre_2']."</td>";
                $html.= "<td align='center'>".$values[$k]['apellido_paterno']."</td>";
                $html.= "<td align='center'>".$values[$k]['apellido_materno']."</td>";
                $html.= "<td align='center'>".$values[$k]['usuario_red']."</td>";
                $evals = $this->empleados_model->read('catalogo_departamentos', 'id', $values[$k]['departamento']);
                $html.= "<td align='center'>".$evals[0]['departamento']."</td>";
                $evals = $this->empleados_model->read('catalogo_puestos', 'id', $values[$k]['puesto']);
                $html.= "<td align='center'>".$evals[0]['puesto']."</td>";
                $html.= "<td align='center'>".$values[$k]['extension']."</td>";

                $html.= "<td>".anchor('empleados/add/'.$id,'<img src="'.$images_path.'edit.gif" border="0" width="22" height="22" />')."</td>";
                $html.= "<td>".anchor('empleados/delete/'.$id,'<img src="'.$images_path.'editdelete.gif" border="0" width="22" height="22" />')."</td>";
                $html.= "</tr>";
            }
        }
        $html.= "</tbody>";
        $html.= "</table>";

        return $html;
    }

    function add($id="") {
        $data = $this->menu_model->general("Equipo Asignado a Empleado");
        $data['site_name'] = "Empleados";
        $data['app_menu'] = $this->menu_model->return_link_menu('cmdb');

        $form_path = "empleados/add/".$id;
        $path = "empleados/index";
        $table = "empleados";
        $where = array("disponibilidad"=>"1");
        $id_name = "usuario_red";

        $fields = $this->empleados_model->fields($table);
        $rules = $this->empleados_model->rules($table, $fields);

        if($this->form_validation->run() == FALSE) {
            $new = 1;
            $values = array();
            foreach($fields as $k=>$v) {
                $values[0][$k] = "";
            }
            $exists = $this->empleados_model->exists($table, $id_name, $id);
            if(!empty($exists)) {
                $new = 0;
                $values = $this->empleados_model->read($table, $id_name, $id);
            }

            $form = "<div>";
            $form .= "Los Campos en <span class='required'>Rojo</span> Son Requeridos!";
            $form .= "</div>";

            $form .= "<div>";
            $form .= validation_errors();
            $form .= "</div>";

            //arreglo para guardar los stvc
            $region_v = $this->empleados_model->get_list('catalogo_regiones', $where, "region");
            if(!empty($region_v)) {
                foreach($region_v as $k=>$v) {
                    $region_id = $region_v[$k]['id']; 
                    $region_nombres = $region_v[$k]['region'];
                    $region_options[$region_id] = $region_nombres;
                }
            }

            //arreglo para guardar los stvc's
            $stvc_v = $this->empleados_model->get_list('catalogo_ciudades', $where, "ciudad");
            if(!empty($stvc_v)) {
                foreach($stvc_v as $k=>$v) {
                    $stvc_id = $stvc_v[$k]['id']; 
                    $stvc_nombres = $stvc_v[$k]['ciudad'];
                    $stvc_options[$stvc_id] = $stvc_nombres;
                }
            }

            //arreglo para guardar los departamentos
            $dep_v = $this->empleados_model->get_list('catalogo_departamentos', $where, "departamento");
            if(!empty($dep_v)) {
                foreach($dep_v as $k=>$v) {
                    $dep_id = $dep_v[$k]['id']; 
                    $dep_nombres = $dep_v[$k]['departamento'];
                    $dep_options[$dep_id] = $dep_nombres;
                }
            }

            //arreglo para guardar los puestos
            $puestos_v = $this->empleados_model->get_list('catalogo_puestos', $where, "puesto");
            if(!empty($puestos_v)) {
                foreach($puestos_v as $k=>$v) {
                    $puestos_id = $puestos_v[$k]['id']; 
                    $puestos_nombres = $puestos_v[$k]['puesto'];
                    $puestos_options[$puestos_id] = $puestos_nombres;
                }
            }

            //Creacion de un nuevo empleado
            $form .= form_open($form_path);
            $form .= form_hidden($id_name, $id);

            $form .= form_label($fields["region"],"region");
            $form .= "<br/>";
            $form .= form_dropdown("region", $region_options, set_value("region", $values[0]["region"]),'id="region"');
            $form .= "<br><br/>";

            $form .= form_label($fields["stvc"],"stvc");
            $form .= "<br/>";
            $form .= form_dropdown("stvc", $stvc_options, set_value("stvc", $values[0]["stvc"]),'id="stvc"');
            $form .= "<br><br/>";

            $form .= form_label($fields["nombre_1"],"nombre_1");
            $form .= "<br/>";
            $form .= form_input(array("name" => "nombre_1", "id" => "nombre_1", "size" => 35), set_value("nombre_1",$values[0]["nombre_1"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["nombre_2"],"nombre_2");
            $form .= "<br/>";
            $form .= form_input(array("name" => "nombre_2", "id" => "nombre_2", "size" => 35), set_value("nombre_2",$values[0]["nombre_2"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["apellido_paterno"],"apellido_paterno");
            $form .= "<br/>";
            $form .= form_input(array("name" => "apellido_paterno", "id" => "apellido_paterno", "size" => 35), set_value("apellido_paterno",$values[0]["apellido_paterno"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["apellido_materno"],"apellido_materno");
            $form .= "<br/>";
            $form .= form_input(array("name" => "apellido_materno", "id" => "apellido_materno", "size" => 35), set_value("apellido_materno",$values[0]["apellido_materno"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["usuario_red"],"usuario_red");
            $form .= "<br/>";
            $form .= form_input(array("name" => "usuario_red", "id" => "usuario_red", "size" => 17), set_value("usuario_red",$values[0]["usuario_red"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["departamento"],"departamento");
            $form .= "<br/>";
            $form .= form_dropdown("departamento", $dep_options, set_value("departamento", $values[0]["departamento"]),'id="departamento"');
            $form .= "<br></br>";

            $form .= form_label($fields["puesto"],"puesto");
            $form .= "<br/>";
            $form .= form_dropdown("puesto", $puestos_options, set_value("puesto", $values[0]["puesto"]),'id="puesto"');
            $form .= "<br></br>";

            $form .= form_label($fields["extension"],"extension");
            $form .= "<br/>";
            $form .= form_input(array("name" => "extension", "id" => "extension", "size" => 10), set_value("extension",$values[0]["extension"]));
            $form .= "<br><br/>";

            $form .= form_submit('submit', 'Aceptar') . nbs(2) . form_reset('reset', 'Reset');
            $form .= form_close();

            $data['content_html'] = $form;
            $this->load->view('template',$data);
        } else {
            $id = $this->input->post($id_name);
            $data = array();
            foreach($fields as $k=>$v) {
                $value = $this->input->post($k);
                $data[$k] = $value;
            }
            $this->empleados_model->save($table, $id_name, $id, $data);
            redirect($path);
        }
    }

    function delete($id="") {
        if($id != "") {
            $table = "empleados";
            $id_name = "usuario_red";
            $result = $this->empleados_model->delete($table, $id_name, $id);
        }
        redirect('empleados/index');
    }
}        

/* end of file */
